package main

import (
	"crypto/rand"
	"encoding/json"
	"fmt"
	"github.com/panjf2000/ants"
	"math/big"
	"net/http"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
	"testing"
	"time"
)

func main() {
	fmt.Println("这里只程序的入口")
	fmt.Println(6 / 3)
	fmt.Println(6 % 3)
	v := "1845453456"
	tmv, err := strconv.ParseUint(strings.TrimSpace(v), 10, 32)
	fmt.Println(tmv, err)
	PageLen, PageNum := CheckPageParse(30, 1)
	fmt.Println(PageNum, PageLen)
	a := "`allow_link` = %d ,"
	a = strings.TrimRight(a, ",")
	fmt.Println(a)
	//rand.Seed(time.Now().UnixNano())
	//fmt.Println(rand.Intn(5))
	//indexArr := rand.Perm(5)
	// fmt.Println("随机数为：",indexArr)
	var hh error
	fmt.Println("hh ", hh)
	fmt.Println("time: ", time.Now().Unix())
	time1 := time.Now().UnixNano() / 1e6
	fmt.Println("time1: ", time1)

	var Arrda []string
	a1 := "hihi"
	b1 := "hdids"
	Arrda = append(Arrda, a1, b1)
	fmt.Println("Arrda:", Arrda)
	fmt.Println("data:", len(""))

	strPayPlat := ""
	payPlatArr := make([]int, 0)
	err = json.Unmarshal([]byte(strPayPlat), &payPlatArr)
	fmt.Println("data11:", err, len(strPayPlat), len(payPlatArr))
	data := make([]string, 0, 0)
	fmt.Println("data: ", data)

	// 测试map的长度
	bbb := make(map[string]interface{})
	bbb["a"] = "bb"
	bbb["c"] = 23
	delete(bbb, "c")
	fmt.Println("bbb的长度为：", len(bbb))
	tmDate := time.Now()
	snow := fmt.Sprintf("%02d:02%d:02%d", tmDate.Hour(), tmDate.Minute(), tmDate.Second())
	tnow, err := time.ParseInLocation("15:04:05", snow, time.Local)
	fmt.Println("data1: ", tnow.Second())
	timeNow := time.Now().Format("15:04:00")
	fmt.Println("timeNow:", timeNow)

	str2 := "啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊"
	fmt.Println("len:", len(str2))

	fmt.Println("当前的时间戳为：", time.Now().UnixNano(), len(string(time.Now().UnixNano())))
	var instArr = []string{"jiji", "jiojo", "jijio", "jioajoi", "jiojio"}
	bidx, err := rand.Int(rand.Reader, big.NewInt(int64(len(instArr))))
	fmt.Println("bidx:", bidx.Int64())
	instArr = nil
	fmt.Println("len:", len(instArr))

	PushDeleteHistory("2020-10-10 01:32:28")

	path, err := filepath.Abs("./")
	pathSlice := strings.Split(path, "\\")
	pathBase := ""
	for i := 0; i < len(pathSlice); i++ {
		pathBase += pathSlice[i]
		pathBase += "/"
	}
	fmt.Println(pathBase)

	s := make(map[int]map[string]string, 0)
	data111, ok := s[12345]["haha"]
	fmt.Println(data111, ok)
	rsl := GetResult()
	switch rsl.(type) {
	case string:
		fmt.Println("a为string类型", rsl.(string))
	case int:
		fmt.Println("a为int类型", rsl.(int))
	default:
		fmt.Println("无法得知a的类型")
	}

	fmt.Println("111111111")
	_ = ants.Submit(func() {
		TestSleep()
	})
	fmt.Println("222222222")
	fmt.Println(time.Now().Format("2006010201"))
	time.Sleep(time.Millisecond * 500)
	var welcomeFlag bool
	fmt.Println(welcomeFlag)

	dataTime := GetTodayRemainingTime(&testing.T{})
	fmt.Println(dataTime / 3600)

	fmt.Println("rls", 1610777010-1610776871)
	asas := time.Unix(1596605313, 64)
	fmt.Println(asas)
	dadadas := -1
	fmt.Println(-dadadas)
	fmt.Println(time.Now())

	times := time.Now().Unix() / 86400
	fmt.Println("times:", times)
	timeEnd := time.Now().Unix()
	timeStart := timeEnd - times - 86400*6
	fmt.Println(timeStart)

	v11 := 3.141532
	fmt.Println(fmt.Sprintf("%f", v11))
	RemoveStr()

	aasss := "  "
	aasss = strings.TrimSpace(aasss)
	fmt.Println(len(aasss))

	v11 = 0
	fmt.Println(v11)
}

func RemoveStr() {
	var s string
	s = "333, "
	s = strings.TrimRight(s, ", ")
	fmt.Println(s)
}

func TestTime() {

	time.After(10)
}

// 获取今天还剩多少秒的时间
func GetTodayRemainingTime(t *testing.T) int64 {
	todayLast := time.Now().Format("2006-01-02") + " 23:59:59"
	todayLastTime, _ := time.ParseInLocation("2006-01-02 15:04:05", todayLast, time.Local)
	remainSecond := todayLastTime.Unix() - time.Now().Local().Unix()
	return remainSecond
}

func TestSleep() string {
	time.Sleep(time.Second * 5)
	return "结束"
}

func GetResult() interface{} {
	return 2
}

// 获取一个到当前时间不小于30天的时间
func PushDeleteHistory(Date string) {
	Date = "2020-08-10 01:32:28"
	nowTime := time.Now()
	oldTime := nowTime.AddDate(0, 0, -30)

	var deleteDate uint32
	if len(Date) == 0 {
		deleteDate = uint32(oldTime.Unix())
	} else {
		loc, err := time.LoadLocation("Local")
		if err != nil {
			deleteDate = uint32(oldTime.Unix())
		}
		// "2020-09-10 01:32:28"
		deleteTime, err := time.ParseInLocation("2006-01-02 15:04:05", Date, loc)
		if err != nil {
			return
		}

		if oldTime.Unix() < deleteTime.Unix() {
			deleteDate = uint32(oldTime.Unix())
		} else {
			deleteDate = uint32(deleteTime.Unix())
		}
	}
	fmt.Println(deleteDate)
	return
}

// 分页参数校验
func CheckPageParse(PageLen, PageNum uint32) (uint32, uint32) {
	if PageNum == 0 {
		PageNum = 1
	}
	if PageLen == 0 {
		PageLen = 20
	}
	if PageLen > 300 {
		PageLen = 300
	}
	return PageLen, PageNum
}

type Man struct {
	name string
}

func ReqPool(url string, data []string, client http.Client) {
	var wg sync.WaitGroup
	pool := &sync.Pool{
		New: func() interface{} {
			fmt.Println("create a new obj.")

			return 100
		},
	}
	pool.Put(10)
	pool.Put(10)
	pool.Put(10)
	for i := 0; i < 10; i++ {
		wg.Add(1)
		go func(id int) {
			defer func() {
				wg.Done()
			}()
			fmt.Println(pool.Get())
		}(i)
	}
	wg.Wait()
}

//type Job struct {
//	Tool *cron.Cron
//}
//
//func (this *Job) DoWorker() {
//	timeNow := time.Now().Format("15:04")
//	fmt.Println("timeNow:", timeNow)
//	fmt.Println("1111111111")
//}
//
//func CronWorker() {
//	job := Job{}
//	job.Tool = cron.New()
//	err := job.Tool.AddFunc("0 */1 * * * *", func() {
//		job.DoWorker()
//	})
//	if err != nil {
//		log.Println("err:", err)
//	}
//	job.Tool.Start()
//	select {}
//}
